#pragma once

#include "Socket.hpp"
#include "Log.hpp"
#include "InetAddr.hpp"
#include "Common.hpp"
#include <memory>
#include <unordered_map>
#include "Connection.hpp"
#include "Epoller.hpp"

using namespace SocketModule;

class TcpServer
{
public:
    TcpServer()
        : _epoller_ptr(std::make_unique<Epoller>())
    {
    }
    void Start()
    {
        while (true)
        {
            _epoller_ptr->Wait();
            sleep(1);
        }
    }

    void AddConnection(const std::shared_ptr<Connection> &conn)
    {
        // 1.添加事件和fd到内核中
        int fd = conn->GetFd();
        uint32_t events = conn->GetEvents();
        _epoller_ptr->ADDEvent(fd, events);

        // 2.添加到_connections中
        _connections[fd] = conn;
    }

    ~TcpServer()
    {
    }

private:
    // 存放fd与连接的映射
    std::unordered_map<int, std::shared_ptr<Connection>> _connections;
    // epoll模型
    std::unique_ptr<Epoller> _epoller_ptr;
};